package com.cskaoyan.app.wxservice.impl;

import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.po.MarketKeyword;
import com.cskaoyan.app.bean.po.MarketKeywordExample;
import com.cskaoyan.app.bean.po.MarketSearchHistory;
import com.cskaoyan.app.bean.po.MarketSearchHistoryExample;
import com.cskaoyan.app.bean.vo.wx.WxSearchIndexVo;
import com.cskaoyan.app.mapper.MarketKeywordMapper;
import com.cskaoyan.app.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.app.wxservice.WxSearchService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public WxSearchIndexVo index() {
        // MarketKeyword defaultKeyword;
        // List<MarketKeyword> hotKeywordList;
        // List<MarketSearchHistory> historyKeywordList;
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo();
        //查询 关键字    默认关键字   热键list   搜索历史关键字

        //1.默认关键字 只需要一个
        MarketKeyword defaultKeyword = null;
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        criteria.andIsHotEqualTo(true);
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> defaultKeywords = marketKeywordMapper.selectByExample(example);
        for (MarketKeyword keyword : defaultKeywords) {
            defaultKeyword = keyword;
            break;
        }
        wxSearchIndexVo.setDefaultKeyword(defaultKeyword);

        //2.热键list
        List<MarketKeyword> hotKeywordList;
        MarketKeywordExample example1 = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria1 = example.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria.andIsHotEqualTo(true);
        List<MarketKeyword> keywordList = marketKeywordMapper.selectByExample(example1);
        hotKeywordList = keywordList;
        wxSearchIndexVo.setHotKeywordList(hotKeywordList);

        //3.用户的搜索历史 list
        //获取用户的id  登录情况 才会去查询历史关键字
        List<MarketSearchHistory> historyKeywordList = new LinkedList<>();
        Subject subject = SecurityUtils.getSubject();
        Integer userid = (Integer) subject.getSession().getAttribute("userid");
        if (userid != null) {
            MarketSearchHistoryExample example2 = new MarketSearchHistoryExample();
            MarketSearchHistoryExample.Criteria criteria2 = example2.createCriteria();
            criteria2.andUserIdEqualTo(userid);
            criteria2.andDeletedEqualTo(false);
            example2.setOrderByClause("update_time desc");
            List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(example2);
            historyKeywordList = marketSearchHistories;
        }
        wxSearchIndexVo.setHistoryKeywordList(historyKeywordList);
        return wxSearchIndexVo;
    }

    @Override
    public void clearhistory() {
        Subject subject = SecurityUtils.getSubject();
        Integer userid = (Integer) subject.getSession().getAttribute("userid");
        //用户登录的情况下  提供删除历史记录的接口
        if (userid != null) {
            MarketSearchHistoryExample example = new MarketSearchHistoryExample();
            MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(userid);
            criteria.andDeletedEqualTo(false);
            MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
            marketSearchHistory.setDeleted(true);
            marketSearchHistoryMapper.updateByExampleSelective(marketSearchHistory,example);
        }
    }

    @Override
    public List<String> helper(String keyword) {

        //新建 list<String>
        List<String> list = new LinkedList<>();
        //查询 未被删除 模糊查询(keyword) 的所有数据

        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andKeywordLike("%"+keyword+"%");
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);
        //赋值给 list<String>  返回
        for (MarketKeyword marketKeyword : marketKeywords) {
            String keyword1 = marketKeyword.getKeyword();
            list.add(keyword1);
        }
        return list;
    }


}
